//HT162X驱动屏的使用接口

#include <stddef.h>
#include "Lcd_AWS_Interface.h"
#include "HT162X_Lcd.h"
#include "HT162X_Parts.h"
#include "../temperature_control_sub_module/FanCtrl.h"
#include "../main.h"
#include "../MCU_Mold/MCU_Time.h"
#include "../CRC16.h"


#define Probe_Max_Settings 300
#define Probe_Min_Settings 50


Lcd_Options_E Lcd_Options;
I_TempCtrl_Param_S I_TempCtrl_Param;
Grill_State_E Grill_state;

Menu_T Cook_Menu = MenuStep_Exit;
uint8_t CooK_MenuItem_Length;


void Time_Set(Lcd_Timer_S timer)
{
    uint32_t x = 0;
    MCU_Time_T tm;
    
    tm.hour = timer.Hour.Get;
    tm.minute = timer.Minute.Get;
    tm.seconds = timer.Second;
    x = TimeConvertTotal(tm);
    Timer1_Setting(x);
    Lcd_Options = Option_P1_Setting;
    Lcd_Timer.isShine_Hour = Lcd_NotShine;
    Lcd_Timer.isShine_Minute = Lcd_NotShine;
    Lcd_Probe1.isShine = Lcd_Shine;
}
void Timer1_Setting(uint32_t x)
{
    UI_System.Timer1.Actual = x;
    UI_System.Timer1.Settings = x;
}
uint32_t Timer1_Downcount_Getting(void)
{
    if(UI_System.Grill_state == State_Grill_Misfire) return 0;

    return UI_System.Timer1.Actual;
}
uint32_t Timer1_GetSetting(void)
{
    if(UI_System.Grill_state == State_Grill_Misfire) return 0;

    return UI_System.Timer1.Settings;
}
void Timer1_Reset(void)
{
    Lcd_Timer.Setting = 0;
    Lcd_Timer.Hour.Get = 0;
    Lcd_Timer.Minute.Get = 0;
    Lcd_Timer.Second = 0;
    Lcd_Timer.Hour.Write = 0;
    Lcd_Timer.Minute.Write = 0;
}

void UI_Alot_StateHandle(Alot_State_E state)
{
    if(state == Alot_AWS_Connected) UI_System.ICO_WIFI = UI_ON;
    else if(state == Alot_AWS_Disconn) UI_System.ICO_WIFI = UI_OFF;
}

void UI_Grill_StateHandle(Grill_State_E state)
{
    Grill_state = state;
    UI_System.Grill_state = state;
}

uint8_t Get_Unit_Grill(void)
{
    return UI_System.Grill_Unit;
}
void UI_SetUnit(uint8_t x)
{
    uint16_t i = 0;

    if((UI_System.Grill_Unit == Unit_F) && (x == Unit_C))
    {
        UI_System.RTD.Settings = Get_C_Gear(UI_System.RTD.Settings, Oven_Setting_Min_C, Oven_Setting_Min_F);
        if(UI_System.ProbeA.Settings > Alarm_Probe_Min) UI_System.ProbeA.Settings = Get_C_Gear(UI_System.ProbeA.Settings, Probe_Setting_Min_C, Probe_Setting_Min_F);
        if(UI_System.ProbeB.Settings > Alarm_Probe_Min) UI_System.ProbeB.Settings = Get_C_Gear(UI_System.ProbeB.Settings, Probe_Setting_Min_C, Probe_Setting_Min_F);
        
        UI_System.Grill_Unit = x;
        ADC_Filter();
        NVS_Write_Unit();
    }
    else if((UI_System.Grill_Unit == Unit_C) && (x == Unit_F))
    {
        UI_System.RTD.Settings = Get_F_Gear(UI_System.RTD.Settings, Oven_Setting_Min_F, Oven_Setting_Min_C);
        if(UI_System.ProbeA.Settings > Alarm_Probe_Min) UI_System.ProbeA.Settings = Get_F_Gear(UI_System.ProbeA.Settings, Probe_Setting_Min_F, Probe_Setting_Min_C);
        if(UI_System.ProbeB.Settings > Alarm_Probe_Min) UI_System.ProbeB.Settings = Get_F_Gear(UI_System.ProbeB.Settings, Probe_Setting_Min_F, Probe_Setting_Min_C);
        
        UI_System.Grill_Unit = x;
        ADC_Filter();
        NVS_Write_Unit();
    }
}

void SetMenuItem(uint8_t seq)
{
    // if(menu_inform[seq][0] == MenuItem_RTD)
    // {
    //     UI_System.RTD.Settings = menu_inform[seq][1];
    //     if(UI_System.RTD.Settings > 500) UI_System.RTD.Settings = 500;
    //     else if(UI_System.RTD.Settings < 165) UI_System.RTD.Settings = 165;
    // }
    // else if(menu_inform[seq][0] == MenuItem_Probe1)
    // {
    //     UI_System.ProbeA.Settings = menu_inform[seq][1];
    //     if(UI_System.ProbeA.Settings > Probe_Setting_Max) UI_System.ProbeA.Settings = Probe_Setting_Max;
    //     else if(UI_System.ProbeA.Settings < Probe_Setting_Min) UI_System.ProbeA.Settings = Probe_Setting_Min;
    // }
    // else if(menu_inform[seq][0] == MenuItem_Probe2)
    // {
    //     UI_System.ProbeB.Settings = menu_inform[seq][1];
    //     if(UI_System.ProbeB.Settings > Probe_Setting_Max) UI_System.ProbeB.Settings = Probe_Setting_Max;
    //     else if(UI_System.ProbeB.Settings < Probe_Setting_Min) UI_System.ProbeB.Settings = Probe_Setting_Min;
    // }
    // else if(menu_inform[seq][0] == MenuItem_Smoke)
    // {
    //     UI_System.RTD.Settings = 165;
    // }
    // else if(menu_inform[seq][0] == MenuItem_Warm)
    // {
    //     UI_System.RTD.Settings = 180;
    // }
    // if(menu_inform[seq][2] > 24) menu_inform[seq][2] = 24;
    // if(menu_inform[seq][3] > 59) menu_inform[seq][3] = 59;
    // UI_System.Timer1.Settings = menu_inform[seq][2] * 3600 + menu_inform[seq][3] * 60;
    UI_System.Timer1.Actual = UI_System.Timer1.Settings;
    UI_System.Grill_state = State_Grill_MenuCook;
}
void UI_SetMenu(void)
{
    uint8_t i;
    uint8_t temp = 0;

    if(UI_System.Grill_state == State_Grill_Misfire) return 0;
    
    if(Cook_Menu == MenuStep_Exit)
    {
        Cook_Menu = MenuStep_1;
        SetMenuItem(Cook_Menu);
    }
    for(i = 0; i < 5; i++)
    {
        // if(menu_inform[i][0] == MenuStep_Exit) temp++;
    }
    if(temp >= 5) Cook_Menu = MenuStep_Exit;
}
void UI_MenuAuto(void)
{
    if(Cook_Menu == MenuStep_1) Cook_Menu = MenuStep_2;
    else if(Cook_Menu == MenuStep_2) Cook_Menu = MenuStep_3;
    else if(Cook_Menu == MenuStep_3) Cook_Menu = MenuStep_4;
    else if(Cook_Menu == MenuStep_4) Cook_Menu = MenuStep_5;
    else if(Cook_Menu == MenuStep_5) Cook_Menu = MenuStep_Exit;
    if(Cook_Menu < CooK_MenuItem_Length) SetMenuItem(Cook_Menu);
    else
    {
        Cook_Menu = MenuStep_Exit;
        if(UI_System.Grill_state == State_Grill_MenuCook) UI_System.Grill_state = State_Grill_Cook;
    }
}
void UI_ProbeA_Setting(uint16_t x)
{
    uint16_t temp = Probe_Setting_Min_F;

    if(UI_System.Grill_Unit == Unit_C) temp = Probe_Setting_Min_C;
    if(x < temp) x = temp;
    UI_System.ProbeA.Settings = x;
}
void UI_ProbeB_Setting(uint16_t x)
{
    uint16_t temp = Probe_Setting_Min_F;

    if(UI_System.Grill_Unit == Unit_C) temp = Probe_Setting_Min_C;
    if(x < temp) x = temp;
    UI_System.ProbeB.Settings = x;
}
uint16_t UI_ProbeA_GetSetting(void)
{
    return UI_System.ProbeA.Settings;
}
uint16_t UI_ProbeB_GetSetting(void)
{
    return UI_System.ProbeB.Settings;
}

uint16_t UI_ProbeA_GetTemp(void)
{
    if(Temp_Param.probe1_Err == Pt_Normal)
    {
        return UI_System.ProbeA.Actual;
    }
    else return 999;
}
uint16_t UI_ProbeB_GetTemp(void)
{
    if(Temp_Param.probe2_Err == Pt_Normal)
    {
        return UI_System.ProbeB.Actual;
    }
    else return 999;
}

uint16_t UI_Home_Get_RTDTemp(void)
{
    return UI_System.RTD.Actual;
}
void I_Lcd_SetRTD(uint16_t x)
{
    uint16_t temp = Oven_Setting_Min_F;

    if(UI_System.Grill_Unit == Unit_C) temp = Oven_Setting_Min_C;
    if(x < temp) x = temp;
    UI_System.RTD.Settings = x;
    ADC_KeyUpDown();

    if((UI_System.Grill_Smoke == State_Grill_Smoke) || (UI_System.Grill_state == State_Grill_KeepWarm))
    {
        UI_System.Grill_state = State_Grill_Ignite;
    } 
}
uint16_t I_Lcd_GetRTDSetting(void)
{
    return UI_System.RTD.Settings;
}

uint32_t UI_Shuttdown_GetTime(void)
{
    if(UI_System.Grill_state == State_Grill_Misfire) return UI_System.Timer1.Actual;
    else return 0;
}

uint8_t Get_State_Heat(void)
{
    if((UI_System.Grill_state >= State_Grill_Ignite) && (UI_System.Grill_state <= State_Grill_MenuCook)) return 1;
    else return 0;
}
uint8_t Get_State_Grill(void)
{
    return UI_System.Grill_state;
}
uint8_t Get_State_Error(void)
{
    return Temp_Param.oven_Err;
}

uint8_t UI_Timer_GetTimer1Alert(void)
{
    return UI_System.Timer1.Alart_State;
}
uint8_t UI_Probe_GetProbeAAlert(void)
{
    return UI_System.ProbeA.Alart_State;
}
uint8_t UI_Probe_GetProbeBAlert(void)
{
    return UI_System.ProbeB.Alart_State;
}

void Subscribe_CmdHandle(MqttSubscribe_Msg_S msg)
{
    static uint16_t param_old = 0;

    if(UI_System.Grill_state == State_Grill_Misfire) return;

    ESP_LOGE("接收 订阅参数ID", "%d", msg.MqttSubscribe_MsgID);
    ESP_LOGE("接收 订阅参数", "%d", msg.result);
    if(msg.MqttSubscribe_MsgID == SubMsgID_SetUnit)
    {
        UI_SetUnit(msg.result);
    }
    else if(msg.MqttSubscribe_MsgID == SubMsgID_SetempRTD0)
    {
        I_Lcd_SetRTD(msg.result);
    }
    else if(msg.MqttSubscribe_MsgID == SubMsgID_SetempRTD1)
    {
        UI_ProbeA_Setting(msg.result);
    }
    else if(msg.MqttSubscribe_MsgID == SubMsgID_SetempRTD2)
    {
        UI_ProbeB_Setting(msg.result);
    }
    else if(msg.MqttSubscribe_MsgID == SubMsgID_SetTimerA)
    {
        Timer1_Setting(msg.result);
    }
    else if(msg.MqttSubscribe_MsgID == SubMsgID_Shutoff)
    {
        if(UI_System.Grill_state != State_Grill_Misfire) UI_TempCtrl_Stop();
    }
    else if(msg.MqttSubscribe_MsgID == SubMsgID_Smoke)
    {
        if(UI_System.Grill_Smoke != State_Grill_Smoke)
        {
            param_old = UI_System.RTD.Settings;
            I_Lcd_SetRTD(msg.result);
            UI_System.Grill_Smoke = State_Grill_Smoke;
        } 
    }
    else if(msg.MqttSubscribe_MsgID == SubMsgID_Keepwarm)
    {
        if(UI_System.Grill_state != State_Grill_KeepWarm)
        {
            param_old = UI_System.RTD.Settings;
            I_Lcd_SetRTD(msg.result);
            UI_System.Grill_state = State_Grill_KeepWarm;
        }
    }
    else if(msg.MqttSubscribe_MsgID == SubMsgID_Ignite)
    { //取消 Smoke, Warm
        if((UI_System.Grill_Smoke == State_Grill_Smoke) || (UI_System.Grill_state == State_Grill_KeepWarm))
        {
            UI_System.Grill_state = State_Grill_Ignite;
            UI_System.Grill_Smoke = State_Grill_Cook;
            if(param_old != 0)
            {
                I_Lcd_SetRTD(param_old);
                param_old = 0;
            }
        } 
    }
}
